var ownedWeaponList = [];
var backgroundColor = {"item-shotgun":"#CA6","item-flamethrower":"#F53","item-healingRay":"#99F",};


$(document).ready(function(){
	var oldPlayerSetData = Player.prototype.setData;
	Player.prototype.setData = function(a, b){
		this.mData[a] = b
		if(a== "weapon")
			updateWeaponOverlay(b);
	};
	createLastVisitButton();
	createFavoriteLocationButton();
	createFavoriteRestaurantButton();
	createLocationButton();
	//total XP toggle
	$('<div class="topMeterDetails landscape">Total XP: <span id="totalXP">0</span> XP</div>').insertAfter(".topMeterDetails").hide();
	$("#levelStatsBlock").click(showTotalXP)
});

function updateWeaponOverlay(a){
    if(ownedWeaponList.length < 1){
		var c = UserCharacter.currentUserCharacter();
		var weaponList = ["item-shotgun", "item-flamethrower", "item-healingRay"];
		
		for (i in weaponList)
		{
			var stock = c.getInventoryCount(weaponList[i]);
			if(stock > 0){
				//Weapon is owned
				ownedWeaponList.push(weaponList[i]);
				if(c.getData("weapon") == weaponList[i]){
					//Weapon is equipped
					createWeaponButton(weaponList[i]);
				}
			}
		}
	}
	updateWeapon(a);
}

function createWeaponButton(a){
    $('<div id="weaponSelect" class="gameMetaControl button platinum gameUI elementsToDisableDuringCombat"></div>').appendTo("#landscapeButtonBar")
		.html('<span class="gloss"></span><span class="icons-dock weaponSelect-tab"></span>');
	$(".weaponSelect-tab").css({"background":"url('/animations/getSpriteSheet/"+Sprite.sAnimationsVersion+"/"+a+".png') 0px 0px", "background-size":"100%", "width": "100%", "height": "30px",});
	$("#weaponSelect").click(toggleWeapon);
}

function createLastVisitButton(){
	$('<div id="lastLocationMenuButton" class="gameMetaControl button platinum gameUI elementsToDisableDuringCombat"></div>').appendTo("#landscapeButtonBar")
		.html('<span class="gloss"></span><div class="icon goto-last-location3 lastLocation-tab"></div>');
	$("#lastLocationMenuButton").click(goToLastLocation);
}

function createFavoriteRestaurantButton(){
	$('<div id="favoriteFoodMenuButton" class="gameMetaControl button platinum gameUI elementsToDisableDuringCombat"></div>').appendTo("#landscapeButtonBar")
		.html('<span class="gloss"></span><span class="icons-dock favoriteFood-tab""></span>');
	$(".favoriteFood-tab").css(
			{
				"background":"url('/animations/getSpriteSheet/"+Sprite.sAnimationsVersion+"/building-restaurant-other-open.png') 0px 0px", 
				"margin-left":"11px", 
				"background-size":"100%", 
				"width": "22px", 
				"height": "32px",
			}
		);
	$("#favoriteFoodMenuButton").click(travelToRestaurant);
}

function createFavoriteLocationButton(){
	$('<div id="favoriteLocationMenuButton" class="gameMetaControl button platinum gameUI elementsToDisableDuringCombat"></div>').appendTo("#landscapeButtonBar")
		.html('<span class="gloss"></span><div class="menuOptionIcon favorites"></div>');
	
	$("#favoriteLocationMenuButton").click(travelToFavoriteLocation);
}

function createLocationButton(){
    $('<div id="locationButton" class="gameMetaControl button platinum gameUI elementsToDisableDuringCombat"></div>').appendTo("#landscapeButtonBar")
		.html('<span class="gloss"></span><span class="icons-dock locationButton-tab"></span>');
	$(".locationButton-tab").css(
			{
				"background":"url('/htmlui/images/sprite-icons3.png') 0px -2211px", 
				"background-size":"100%", 
				"width": "45px", 
				"height":"30px",
				"margin":"-2px 0px 0px 6px",
			}
		);
	window.popupCurrentLocation && $("#locationButton").click(popupCurrentLocation);
}

function updateWeapon(a){
	$(".weaponSelect-tab").css({"background-image":"url('/animations/getSpriteSheet/"+Sprite.sAnimationsVersion+"/"+a+".png')"});
	$("#weaponSelect").css({"background":backgroundColor[a]});
}

function toggleWeapon(){
	var current = ownedWeaponList.indexOf(UserCharacter.currentUserCharacter().getData("weapon"));
	var next = (current + 1) % ownedWeaponList.length
	buyItem(ownedWeaponList[next]);
	$(".popupAlert").draggable(false);
}

function travelToRestaurant(){
	travelToFavorite(0);
}

function travelToFavoriteLocation(){
	travelToFavorite(1);
}

function travelToFavorite(a){
	var location = favoriteLocations[a];
	if(location && location!="0")
	{
		$('<input type="text" id="travelSubmitValue" style="display:none;"/>').appendTo("body");
		$("#travelSubmitValue").val(location);
		codeAddress();
		$("#travelSubmitValue").remove();
	}
	else
	{
		showPopupAlert("Custom Location Button", "Set locations for custom buttons in Fleck Landscape's Extension settings.<br>Use the find me button to get the coordinates of the location you want to favorite to copy into the settings or use any location you can type into travel" , null, null, null);
	}
}

function showTotalXP(){
	if($("#totalXP").is(":hidden"))
	{
		$("#levelStatsBlock .topMeterDetails").not(".landscape").fadeOut(100).delay(10300).fadeIn(100);
		$("#levelStatsBlock .topMeterDetails.landscape").delay(100).fadeIn(100).delay(10000).fadeOut(100);
		$("#totalXP").html(Util.formatNumber(UserCharacter.currentUserCharacter().getXp()));
	}
	else
	{
		$("#totalXP").html(Util.formatNumber(UserCharacter.currentUserCharacter().getXp()));
	}
}